package App.OO.service;

import App.Model.Interface.MessageType;
import App.Model.message.Message;
import App.OO.service.thread.Manage.ManageCtoSThread;

import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Date;

//聊天相关服务
public class OOTalkService {
    Socket socket = null;

    public static void getOfflineMessage(String userId) {
        //获取离线消息
        Message message = new Message();
        message.setSender(userId);
        message.setGetter("服务器");
        message.setSendTime(new Date());
        message.setMesType(MessageType.MESSAGE_COMM_MSG_OFFLINE);
        //发送向服务器
        try {
            ObjectOutputStream oos =
                    new ObjectOutputStream((ManageCtoSThread.getCtoSThread(userId)).getSocket().getOutputStream());
            oos.writeObject(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public void getUserDetail(String aimUserName, String senderId) {
        //获取某个用户的用户信息
        Message message = new Message();
        message.setSender(senderId);
        message.setContent(aimUserName);
        message.setGetter("服务器");//设置为服务器是因为如果消息意外泄漏可以发现原因
        message.setMesType(MessageType.MESSAGE_GET_MSG_NOW_USER);

        try {
            ObjectOutputStream oos =
                    new ObjectOutputStream((ManageCtoSThread.getCtoSThread(senderId)).getSocket().getOutputStream());
            oos.writeObject(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sendTalkOne(String content, String senderId, String getterId) {
        //私聊
        Message message = new Message();
        message.setContent(content);
        message.setSender(senderId);
        message.setGetter(getterId);
        message.setSendTime(new Date());
        message.setMesType(MessageType.MESSAGE_COMM_MSG);
        System.out.println(senderId + " 对 " + getterId + " 说 " + content);

        try {
            ObjectOutputStream oos =
                    new ObjectOutputStream((ManageCtoSThread.getCtoSThread(senderId)).getSocket().getOutputStream());
            oos.writeObject(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sendTalkOne(Message packageMessage) {
        //私聊,打包的message
//        System.out.println
//                (packageMessage.getSender() + " 对 " + packageMessage.getGetter() + " 说 " + packageMessage.getContent());
        try {
            ObjectOutputStream oos =
                    new ObjectOutputStream(
                            (ManageCtoSThread.getCtoSThread(packageMessage.getSender()))
                                    .getSocket()
                                    .getOutputStream());
            oos.writeObject(packageMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sendTalkAll(String content, String senderId) {
        //群发
        Message message = new Message();
        message.setContent(content);
        message.setSender(senderId);
        message.setSendTime(new Date());
        message.setMesType(MessageType.MESSAGE_COMM_MSG_ALL);

        try {
            ObjectOutputStream oos =
                    new ObjectOutputStream((ManageCtoSThread.getCtoSThread(senderId)).getSocket().getOutputStream());
            oos.writeObject(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
